package com.zk.qqrobot.robot.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.forte.qqrobot.anno.Filter;
import com.forte.qqrobot.anno.Listen;
import com.forte.qqrobot.anno.depend.Beans;
import com.forte.qqrobot.anno.depend.Depend;
import com.forte.qqrobot.beans.messages.msgget.GroupMemberIncrease;
import com.forte.qqrobot.beans.messages.msgget.GroupMsg;
import com.forte.qqrobot.beans.messages.types.MsgGetTypes;
import com.forte.qqrobot.beans.types.KeywordMatchType;
import com.forte.qqrobot.sender.MsgSender;
import com.forte.qqrobot.utils.CQCodeUtil;
import com.zk.qqrobot.config.Constant;
import com.zk.qqrobot.config.SSRUrlEnum;
import com.zk.qqrobot.model.Reward;
import com.zk.qqrobot.service.SSRService;
import com.zk.qqrobot.util.RestUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Beans
@Component
public class GroupListener {
    private SSRService ssrService = new SSRService();
    private RestTemplate restTemplate = new RestTemplate();

    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "^#.*", keywordMatchType = KeywordMatchType.REGEX)
    public void onMessage(GroupMsg msg, MsgSender sender, CQCodeUtil cqCodeUtil){
        if (msg.getMsg().length() == 1){
            return;
        }
        String text = msg.getMsg().substring(1);
        SSRUrlEnum ssrUrlEnum = null;
        if(text.equals("s1")){
            ssrUrlEnum = SSRUrlEnum.SSR_URL_4;
        }else if(text.equals("s2")){
            ssrUrlEnum = SSRUrlEnum.SSR_URL_5;
        }else if(text.equals("s3")){
            ssrUrlEnum = SSRUrlEnum.SSR_URL_1;
        }else if(text.equals("s4")){
            ssrUrlEnum = SSRUrlEnum.SSR_URL_2;
        }else if(text.equals("s5")){
            ssrUrlEnum = SSRUrlEnum.SSR_URL_3;
        }
        String respText = null;

        respText = restTemplate.getForObject("http://localhost:9001/ssr/findSsrCatch?urlCode=" + ssrUrlEnum.getCode(), String.class);
        if (respText == null){
            respText = ssrService.getBaseMode(ssrUrlEnum);
            Map<String,Object> param = new HashMap<>();
            param.put("urlCode",ssrUrlEnum.getCode().toString());
            param.put("ssr",respText);
            RestUtil.forPost("http://localhost:9001/ssr/ssrCatch",param);
        }
        System.out.println("send:" + sender.SENDER.sendGroupMsg(msg.getGroup(),respText));
    }

    @Listen(MsgGetTypes.groupMemberIncrease)
    public void GroupMemberIncrease(GroupMemberIncrease msg, MsgSender sender, CQCodeUtil cqCodeUtil){
        String so = restTemplate.getForObject("http://localhost:9001/reward/top5?group=" + msg.getGroup(), String.class);
        sender.SENDER.sendGroupMsg(msg.getGroup(),cqCodeUtil.getCQCode_at(msg.getBeOperatedQQ()) + Constant.GROUP_INC + "\n" + so);
    }

    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "^admin:.*", keywordMatchType = KeywordMatchType.REGEX)
    public void reward(GroupMsg msg, MsgSender sender, CQCodeUtil cqCodeUtil){
        if (msg.getQQ().equals(Constant.ADMIN_QQ)||msg.getQQ().equals(Constant.ADMIN_QQ_TEMP)){
            String str = msg.getMsg();
            String[] split = str.split(":");
            String nick = split[1];
            String amount = split[2];
            restTemplate.getForObject("http://localhost:9001/reward/save?nick=" + nick + "&amount=" + amount + "&group=" + msg.getGroup() , String.class);
            String so = restTemplate.getForObject("http://localhost:9001/reward/top5?group=" + msg.getGroup(), String.class);
            String sss = "【打赏排行榜】\n" + so ;
            sender.SENDER.sendGroupMsg(msg.getGroup(),sss);
        }else{
            sender.SENDER.sendGroupMsg(msg.getGroup(),cqCodeUtil.getCQCode_at(msg.getQQ())+"\n你要闹哪样？");
        }
    }
    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "打赏榜", keywordMatchType = KeywordMatchType.REGEX)
    public void findReward(GroupMsg msg, MsgSender sender, CQCodeUtil cqCodeUtil){
        String so = restTemplate.getForObject("http://localhost:9001/reward/top5?group=" + msg.getGroup(), String.class);
        String sss = "【打赏排行榜】\n" + so;
        sender.SENDER.sendGroupMsg(msg.getGroup(),sss);
    }
    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "打赏", keywordMatchType = KeywordMatchType.REGEX)
    public void toReward(GroupMsg msg, MsgSender sender, CQCodeUtil cqCodeUtil){
        sender.SENDER.sendGroupMsg(msg.getGroup(),Constant.TO_REWARD);
    }

    @Listen(MsgGetTypes.groupMsg)
    public void activity(GroupMsg msg, MsgSender sender, CQCodeUtil cqCodeUtil){
        restTemplate.getForObject("http://localhost:9001/activity/save?qq="+msg.getQQ() + "&group=" + msg.getGroup(),String.class);
    }

//    @Listen(MsgGetTypes.groupMsg)
//    @Filter(value = "活跃榜", keywordMatchType = KeywordMatchType.REGEX)
//    public void activitys(GroupMsg msg, MsgSender sender, CQCodeUtil cqCodeUtil){
//        String str = restTemplate.getForObject("http://localhost:9001/activity/acs?group=" + msg.getGroup(), String.class);
//        sender.SENDER.sendGroupMsg(msg.getGroup(),"【活跃度榜单】\n" + str);
//    }

    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "^v.*", keywordMatchType = KeywordMatchType.REGEX)
    public void video(GroupMsg msg, MsgSender sender, CQCodeUtil cqCodeUtil){
        String str = msg.getMsg();
        String substring = str.substring(2, str.length());
        sender.SENDER.sendGroupMsg(msg.getGroup(),"VIP视频解析地址：\n" + Constant.VIDEO_URL.concat(substring));
    }

    @Listen(MsgGetTypes.groupMsg)
    @Filter(value = "^test.*", keywordMatchType = KeywordMatchType.REGEX)
    public void test(GroupMsg msg, MsgSender sender, CQCodeUtil cqCodeUtil){
        System.out.println(cqCodeUtil.getCQCode_at(msg.getQQ()));
        sender.SENDER.sendGroupMsg(msg.getGroup(),cqCodeUtil.getCQCode_at(msg.getQQ()));
    }
}
